/**
 * Copyright 2013 Net355 Co,.Ltd. 
 * All rights reserved.
 *
 * "ServiceController.java is for what: "
 * 
 * Create by Ray at Dec 23, 2013 9:29:13 PM.
 * Ray [email:rayinhangzhou@126.com]
 * 
 * Revision History
 *
 * Date            Programmer                   Notes
 * ---------    ---------------------  -----------------------------------
 * Dec 23, 2013            Ray                       initial
 **/


package com.net355.eblog.controller;


import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.net355.eblog.models.EbNote;
import com.net355.eblog.models.EbNotebook;
import com.net355.eblog.models.EbTag;
import com.net355.eblog.util.CacheHelper;
import com.net355.eblog.util.DateToolkit;
import com.net355.eblog.util.GlobalParams;
import com.net355.eblog.util.StaticHtmlHelper;
import com.net355.eblog.util.ThreadHelper;

/**
 * 静态页面模块
 * @author ray
 *
 */
@Controller
public class PageController extends BaseController {


	@SuppressWarnings("unchecked")
	@RequestMapping(value="/index")
	public ModelAndView getIndexPage(ModelAndView mav) {
		List<String> scoreList = (List<String>) CacheHelper.get_cache(GlobalParams.CACHE_NAME_LIST_GAME_SCORE);
		if(scoreList.size()<8) {
			mav.addObject("scoreLine", 0);
		} else {
			mav.addObject("scoreLine", scoreList.get(scoreList.size()-1).split("_")[1]);
		}
		mav.setViewName("/index.jsp");
		return mav;
	}
	
	@RequestMapping(value="/notebook")
	public ModelAndView getNotebookPage(ModelAndView mav) {
		List<?> notebooks = (List<?>) CacheHelper.get_cache(GlobalParams.CACHE_NAME_LIST_NOTEBOOK);
		String notebookGuid = (String)notebooks.get(notebooks.size()-1);
		EbNotebook notebook = (EbNotebook)CacheHelper.get_cache(notebookGuid);
		mav.addObject("pageIndex", 1);
		mav.addObject("noteList", StaticHtmlHelper.getNoteListByNoteGuids(notebook.getNotes(), 0, GlobalParams.PAGE_SIZE));
		mav.addObject("notebook", notebook.getName());
		mav.addObject("notebookGuid", notebookGuid);
		mav.addObject("notesize", notebook.getNotes().size());
		mav.setViewName("/notebook.jsp");
		return mav;
	}
	@RequestMapping(value="/notebook/{guid}")
	public ModelAndView getPageNotebook(ModelAndView mav, @PathVariable("guid") String notebookGuid) {
		EbNotebook notebook = (EbNotebook)CacheHelper.get_cache(notebookGuid);
		if(notebook==null) {
			mav.setViewName("/resource/pages/404.html");
			return mav;
		}
		mav.addObject("pageIndex", 1);
		mav.addObject("noteList", StaticHtmlHelper.getNoteListByNoteGuids(notebook.getNotes(), 0, GlobalParams.PAGE_SIZE));
		mav.addObject("notebook", notebook.getName());
		mav.addObject("notebookGuid", notebookGuid);
		mav.addObject("notesize", notebook.getNotes().size());
		mav.setViewName("/notebook.jsp");
		return mav;
	}
	
	@RequestMapping(value="/note")
	public ModelAndView getNotePage(ModelAndView mav) {
		List<?> notes = (List<?>) CacheHelper.get_cache(GlobalParams.CACHE_NAME_LIST_NOTE);
		String noteGuid = (String)notes.get(notes.size()-1);
		EbNote note = (EbNote) CacheHelper.get_cache(noteGuid);
		mav.addObject("noteGuid", noteGuid);
		mav.addObject("noteTitle", note.getTitle());
		mav.addObject("noteUpdate", DateToolkit.formatDate(new Date(note.getUpdateTime()), "yyyy-MM-dd HH:mm:ss"));
		mav.addObject("noteTags", StaticHtmlHelper.getTagListByNote(note));
		mav.addObject("noteContent", CacheHelper.get_cache_html(noteGuid));
		mav.addObject("keyTags", StaticHtmlHelper.getTagsForKeyByNote(note));
		mav.setViewName("/note.jsp");
		return mav;
	}
	@RequestMapping(value="/note/{guid}")
	public ModelAndView getNotePage(ModelAndView mav, @PathVariable("guid") String noteGuid) {
		EbNote note = (EbNote) CacheHelper.get_cache(noteGuid);
		if(note==null) {
			mav.setViewName("/resource/pages/404.html");
			return mav;
		}
		mav.addObject("noteGuid", noteGuid);
		mav.addObject("noteTitle", note.getTitle());
		mav.addObject("noteUpdate", DateToolkit.formatDate(new Date(note.getUpdateTime()), "yyyy-MM-dd HH:mm:ss"));
		mav.addObject("noteTags", StaticHtmlHelper.getTagListByNote(note));
		mav.addObject("noteChat", CacheHelper.get_cache(noteGuid + GlobalParams.CACHE_NAME_LIST_NOTE_COMMENT) == null ? 0
				: ((List<?>) CacheHelper.get_cache(noteGuid + GlobalParams.CACHE_NAME_LIST_NOTE_COMMENT)).size());
		mav.addObject("noteView", note.getViewCount());
		mav.addObject("noteContent", CacheHelper.get_cache_html(noteGuid));
		mav.addObject("keyTags", StaticHtmlHelper.getTagsForKeyByNote(note));
		mav.setViewName("/note.jsp");
		GlobalParams.THREAD_POOL.execute(ThreadHelper.incrementNoteView(noteGuid));
		return mav;
	}
	
	@RequestMapping(value="/chat")
	public ModelAndView getChatPage(ModelAndView mav) {
		List<?> chats = (List<?>) CacheHelper.get_cache(GlobalParams.CACHE_NAME_LIST_CHAT_SYSTEM);
		int size = chats==null?0:chats.size();
		int pageIndex = (int)Math.ceil(size*1.0/GlobalParams.PAGE_SIZE);
		mav.addObject("pageIndex", pageIndex);
		mav.addObject("chatSize", size);
		mav.setViewName("/chat.jsp");
		return mav;
	}
	
	@RequestMapping(value="/tags")
	public ModelAndView getTagsPage(ModelAndView mav) {
		List<?> tags = (List<?>) CacheHelper.get_cache(GlobalParams.CACHE_NAME_LIST_TAG);
		mav.addObject("tagSize", tags.size());
		mav.addObject("tagList", StaticHtmlHelper.getTagListByTagGuids(tags));
		mav.setViewName("/tagview.jsp");
		return mav;
	}
	@RequestMapping(value="/tag/{guid}")
	public ModelAndView getTagsPage(ModelAndView mav, @PathVariable("guid") String tagGuid) {
		EbTag tag = (EbTag) CacheHelper.get_cache(tagGuid);
		if(tag==null) {
			mav.setViewName("/resource/pages/404.html");
			return mav;
		}
		mav.addObject("pageIndex", 1);
		mav.addObject("noteList", StaticHtmlHelper.getNoteListByNoteGuids(tag.getNotes(), 0, GlobalParams.PAGE_SIZE));
		mav.addObject("tagName", tag.getName());
		mav.addObject("tagClick", tag.getClickCount());
		mav.addObject("tagUpdate", DateToolkit.formatDate(new Date(tag.getUpdateTime()), "yyyy-MM-dd HH:mm:ss"));
		mav.addObject("tagGuid", tagGuid);
		mav.addObject("noteSize", tag.getNotes().size());
		GlobalParams.THREAD_POOL.execute(ThreadHelper.incrementTagClick(tagGuid));
		mav.setViewName("/tags.jsp");
		return mav;
	}
	
	@RequestMapping(value="/search")
	@SuppressWarnings("unchecked")
	public ModelAndView getSearchPage(ModelAndView mav) {
		List<String> notes = (List<String>) CacheHelper.get_cache(GlobalParams.CACHE_NAME_LIST_NOTE);
		mav.addObject("noteList", StaticHtmlHelper.getNoteListByNoteGuidsAndMatch(notes, null, 0, GlobalParams.PAGE_SIZE).getMessage());
		mav.addObject("noteSize", notes.size());
		mav.setViewName("/search.jsp");
		return mav;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
}
